<?php
class MeAction extends CommonAction {
	//修改个人信息，显示信息页面
	public function index(){
        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
        $this->levelnum=D('Member')->where('id = '.$myid)->getField('level');
        if(!session(C('ADMIN_AUTH_KEY'))&&$this->levelnum == 1) {
            $supplierid = M('Supplier')->where('member_id = ' . $myid)->getField('id');
            $this->redirect(U("Supplier/edit",array('id'=>$supplierid)));
        }else{
            $data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
            $this->info = M('Member') -> where($data) -> find();
            //推荐人信息
            $adb = M('Member_connect');
            $this->aff = $adb -> alias('a') -> join('left join __MEMBER__ as m on m.id = a.member_id') -> where(array('a.referee_id' => $this->info['id'])) -> Field('m.*') -> find();

        }
		$this->display();
	}
	//更新个人信息
	public function update(){
		$this->dao = D('Member');
		$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		$this->info = M('Member') -> where($data) -> find();
		$_POST['id'] = $data['id'];
		
        if ($this->_post('id')) {
            unset($_POST['site_id']);
            $map['id'] = array(
                'eq',
                $_POST['id']
            );
            
            //dump($data);exit();
            //dump(I('post.'));exit();
            $result = $this->dao->save($_POST);
            //echo $this->dao->getLastSql();exit();
            if (false !== $result) {
				
                $this->success('更新成功', $jumpUrl);
            } else {
                $this->error($this->dao->getError());
            }
        } else {
            $this->error('参数错误');
        }
	}
	
	public function edit(){
	    $data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		$this->info = M('Member') -> where($data) -> find();
		$this->display();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	// 生成绑定微信号二维码
    public function createBindQrcode()
    {
        Vendor('Weixin.Weixin');
        $d['token'] = getSysVal('token');
        $d['appid'] = getSysVal('appid');
        $d['appsecret'] = getSysVal('appsecret');
        $this->weixin = new WeixinChat($d);
        
        $affid = 'bind_' . $this->memberInfo['id'] . 'bind_' . 143;
        
        // exit($affid);
        
        $tiket = $this->weixin->createQrcode($affid, 1);
        $this->qrocde = 'http://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $tiket['ticket']; // exit($qrocde);
        $this->display();
        // die(json_encode(['status'=>1,'tbody'=>$qrocde]));
    }
    // 查询绑定状态
    public function bindWX()
    {
        $openid = $this->dao->where([
            'id' => $this->memberInfo['id']
        ])->getField('openid');
        $status = empty($openid) ? 0 : 1;
        echo (json_encode([
            'status' => $status,
            'tbody' => '操作成功'
        ]));
    }
    // 解除当前账号的微信绑定
    public function unBindWX()
    {
        $data['id'] = $this->memberInfo['id'];
        $data['openid'] = '';
        $this->dao->save($data);
        echo (json_encode([
            'status' => 1,
            'tbody' => '操作成功'
        ]));
    }
	//生成绑定微信号临时二维码
	public function createBindTemporaryQrcode(){
		$type = $this->_param('type')?$this->_param('type'):1;
		$expire = $this->_param('expire')?$this->_param('expire'):1800;
        Vendor('Weixin.Weixin');
        $d['token'] = getSysVal('token');
        $d['appid'] = getSysVal('appid');
        $d['appsecret'] = getSysVal('appsecret');
        $this->weixin = new WeixinChat($d);
        
        $affid = 'bind_' . $this->memberInfo['id'] . 'bind_' . 143;
        
        // exit($affid);
        
        $tiket = $this->weixin->createQrcode($affid, $type, $expire);
        $qrocde = 'http://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $tiket['ticket']; // exit($qrocde);
		$return['status'] = 1;
		$return['tbody'] = $qrocde;
		$this->ajaxReturn($return,json);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}